package updatepackage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javabean.Project;

public class Updatepro extends Update{
    public void updateDB(Project pro){
        Connection conn = null;
        PreparedStatement pstmt = null;
        
        try {
            conn = getConnection();
            
           	pstmt = conn.prepareStatement(
              "update PROJECT set proNAME=?, proESTART=?, proEEND=?, proECOST=?, proTEXT=?, proENVIR=?, proMETHOD=?, proASTART=?, proAEND=?, proACOST=?, empID=?, proCOUNT=? "+
              "where proNO=?");
            
            pstmt.setString(1, pro.getProNAME());
            pstmt.setString(2, pro.getProESTART());
            pstmt.setString(3, pro.getProEEND());
            pstmt.setInt(4, pro.getProECOST());
            pstmt.setString(5, pro.getProTEXT());
            pstmt.setString(6, pro.getProENVIR());
            pstmt.setString(7, pro.getProMETHOD());
            pstmt.setString(8, pro.getProASTART());
            pstmt.setString(9, pro.getProAEND());
            pstmt.setInt(10, pro.getProACOST());
            pstmt.setString(11, pro.getEmpID());
            pstmt.setInt(12, pro.getProCOUNT());
            pstmt.setInt(13, pro.getProNO());
            
            pstmt.executeUpdate();
        } catch(SQLException ex) {
            //throw new ProjectManagerException("updateProject", ex);
        } finally {
            if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
            if (conn != null) try { conn.close(); } catch(SQLException ex) {}
        }
    }
    public void updateDB(int proNO, String date){
    	Connection conn1 = null;
    	Connection conn2 = null;
        PreparedStatement pstmt1 = null;
        PreparedStatement pstmt2 = null;
        ResultSet rs = null;

        try {
            conn1 = getConnection();
            conn2 = getConnection();
            pstmt1 = conn1.prepareStatement("select * from PROJECT where proNO = ?");
            pstmt1.setInt(1, proNO);
            rs = pstmt1.executeQuery();
            rs.next();
            if(rs.getString("proASTART").compareTo("0000-00-00") == 0){
            	pstmt2 = conn2.prepareStatement("update PROJECT set proASTART=?"+"where proNO=?");
  	            
      	        pstmt2.setString(1, date);
      	        pstmt2.setInt(2, proNO);
      	        
      	        pstmt2.executeUpdate();
            }
            else {
            	pstmt2 = conn2.prepareStatement("update PROJECT set proAEND=?"+"where proNO=?");
  	            
  	            pstmt2.setString(1, date);
  	            pstmt2.setInt(2, proNO);
  	            
  	            pstmt2.executeUpdate();
            }                        
        } catch(SQLException ex) {
            //throw new ProjectManagerException("updateProject", ex);
        } finally {
        	if (rs != null) try { rs.close(); } catch(SQLException ex) {}
            if (pstmt1 != null) try { pstmt1.close(); } catch(SQLException ex) {}
            if (pstmt2 != null) try { pstmt2.close(); } catch(SQLException ex) {}
            if (conn1 != null) try { conn1.close(); } catch(SQLException ex) {}
            if (conn2 != null) try { conn2.close(); } catch(SQLException ex) {}
        }
    }
}